package org.eclipse.equinox.spring.osgi;
/**
 * This file is part of org.springframework.equinox.deployer. 
 * Copyright (C) olivier.evalet@genebio.com and genebio team
 * 
 * org.springframework.equinox.deployer is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 * 
 * org.springframework.equinox.deployer is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * org.springframework.equinox.deployer. If not, see <http://www.gnu.org/licenses/>.
 */

import javax.servlet.ServletContext;

import org.osgi.framework.BundleContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextException;
import org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;


public class OsgiContextLoader extends ContextLoader {

	
	private BundleContext bundleContext;
	private ApplicationContext appContext;

	public OsgiContextLoader(ApplicationContext appContext) {
		this.appContext=appContext;
	}
	
	protected ApplicationContext loadParentContext(ServletContext servletContext)
	throws BeansException {
		return appContext;
	}	
	protected WebApplicationContext createWebApplicationContext(
			ServletContext servletContext, ApplicationContext parent) throws BeansException {

//		Class contextClass = OsgiBundleXmlWebApplicationContext.class;
//		if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
//			throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
//					"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
//		}
//
//		OsgiBundleXmlWebApplicationContext wac =
//				(OsgiBundleXmlWebApplicationContext) BeanUtils.instantiateClass(contextClass);
//		wac.setParent(parent);
//		wac.setServletContext(servletContext);
//		wac.setConfigLocation("META-INF/web/*.xml");
//		wac.setBundleContext(bundleContext);
//		wac.refresh();
//		
//
//		return wac;
		return new WrappedApplicationContext(parent, servletContext);
	}

	public void initWebApplicationContext(ServletContext servletContext,
			BundleContext bundleContext) {
		this.bundleContext=bundleContext;
		initWebApplicationContext(servletContext);
		
	}	
}
